package cn.antblog.thread;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author YangJingyi
 * @date 2021-09-14 17:19
 * 通过实现Callable接口创建多线程
 *  	1.可以有返回值
 *   	2.可以抛出异常
 */
public class CallableThread {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        //创建线程  个数2
        ExecutorService ser = Executors.newFixedThreadPool(2);
        //实现callable接口类
        Race tortoise  = new Race("乌龟", 100L);
        Race rabbit   = new Race("兔子", 80L);
        //获取值
        Future<Integer> res1 = ser.submit(tortoise);
        Future<Integer> res2 = ser.submit(rabbit);
        Thread.sleep(5000);
        tortoise.setFlag(false);
        rabbit.setFlag(false);

        int num1 = res1.get();
        int num2 = res2.get();

        System.out.println("乌龟跑了"+num1);
        System.out.println("兔子跑了"+num2);
        //停止服务
        ser.shutdownNow();
    }
}
